package com.py.comment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.py.apis.article.IuserClitent;
import com.py.comment.mapper.CommentReplyMapper;
import com.py.comment.service.CommentReplyService;
import com.py.model.common.dtos.ResponseResult;
import com.py.model.moment.dtos.CommentReplyDto;
import com.py.model.moment.dtos.CommentReplyLikeDto;
import com.py.model.moment.dtos.CommentReplySaveDto;
import com.py.model.moment.pojos.Comment;
import com.py.model.moment.pojos.Reply;
import com.py.model.moment.pojos.ReplyLike;
import com.py.model.user.pojos.ApUser;
import com.py.utils.common.ApUserThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author pengy
 * @create 2022-11-17 17:41
 */
@Service
@Slf4j
public class CommentReplyServiceImpl extends ServiceImpl<CommentReplyMapper, Reply> implements CommentReplyService {

    @Autowired
    private IuserClitent iuserClitent;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 回复评论的保存回复评论的保存
     *
     * @param replySaveDto
     * @return
     */
    @Override
    public void saveReply(CommentReplySaveDto replySaveDto) {
        //1校验参数
        if (replySaveDto == null) {
            log.info("数据为空");
            return;
        }
        //2保存评论回复
        Integer userId = ApUserThreadLocalUtil.get().getId();
        //查询获取apuser详细信息
        ApUser apUser = iuserClitent.get(userId);

        Reply reply = new Reply();
        reply.setCommentId(replySaveDto.getCommentId());
        reply.setContent(replySaveDto.getContent());
        reply.setUserId(userId);
        reply.setAuthorName(apUser.getName());
        reply.setImage(apUser.getImage());
        reply.setLikes(0);
        reply.setCreatedTime(new Date());
        reply.setUpdatedTime(new Date());
        reply.setOperation(1);

        mongoTemplate.save(reply);
        //保存了回复评论就证明已经回复了,需要找到被回复的评论,将回复数量加 1
        Comment comment = mongoTemplate.findById(replySaveDto.getCommentId(), Comment.class);
        comment.setReply(comment.getReply() + 1);
        mongoTemplate.save(comment);
    }


    /**
     * 加载回复评论列表 分页展示
     *
     * @param commentReplyDto
     * @return
     */
    @Override
    public ResponseResult loadPage(CommentReplyDto commentReplyDto) {
        //如果是以第一次过来查询 那么传递一个null时间
        if (commentReplyDto.getMinDate() == null) {
            commentReplyDto.setMinDate(new Date());
        }
        //查询条件1
        Query query = Query.query(Criteria.where("commentId").is(commentReplyDto.getCommentId())
                .and("createdTime").lt(commentReplyDto.getMinDate()));

        //按回复时间降序排序
        Sort sort = Sort.by(Sort.Direction.DESC, "createdTime");
        query.with(sort);
        //默认查询十条
        query.limit(10);
        List<Reply> replies = mongoTemplate.find(query, Reply.class);
        return ResponseResult.okResult(replies);
    }


    /**
     * 回复评论的评论点赞或取消点赞
     *
     * @param replyLikeDto
     * @return
     */
    @Override
    public ResponseResult like(CommentReplyLikeDto replyLikeDto) {
        Integer userId = ApUserThreadLocalUtil.get().getId();

        //校验参数
        if (replyLikeDto.getOperation() == 0) {

            //是点赞 先查询 如果有点赞记录 不用点赞了

            Query query = Query.query(Criteria.where("userId").is(userId)
                    .and("replyId").is(replyLikeDto.getCommentRepayId()));
            List<ReplyLike> replyLikes = mongoTemplate.find(query, ReplyLike.class);
            if (replyLikes != null && replyLikes.size() > 0) {
                //证明已经点赞了 删除记录
                mongoTemplate.remove(query, ReplyLike.class);
                //返回
                Reply reply = mongoTemplate.findById(replyLikeDto.getCommentRepayId(), Reply.class);
                return ResponseResult.okResult(reply);
            }

            //没有点赞保存点赞表  将点赞数量加 1
            ReplyLike replyLike = new ReplyLike();
            replyLike.setReplyId(replyLikeDto.getCommentRepayId());
            replyLike.setUserId(userId);
            mongoTemplate.save(replyLike);

            //找到这条回复 将点赞数量加 1
            Reply reply = mongoTemplate.findById(replyLikeDto.getCommentRepayId(), Reply.class);
            reply.setLikes(reply.getLikes() + 1);
            reply.setOperation(replyLikeDto.getOperation());
            mongoTemplate.save(reply);
        } else {
            //取消点赞
            //删除这条点赞记录
            Query query = Query.query(Criteria.where("userId").is(userId)
                    .and("replyId").is(replyLikeDto.getCommentRepayId()));
            mongoTemplate.remove(query, ReplyLike.class);


            //找到这个评论点赞数减 1
            Reply reply = mongoTemplate.findById(replyLikeDto.getCommentRepayId(), Reply.class);
            reply.setLikes(reply.getLikes() - 1);
            reply.setOperation(replyLikeDto.getOperation());
            mongoTemplate.save(reply);

        }
        Reply reply = mongoTemplate.findById(replyLikeDto.getCommentRepayId(), Reply.class);
        return ResponseResult.okResult(reply);
    }
}
